package com.longxi.tianjintianbao.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.model.Personnel;

import java.util.ArrayList;
import java.util.List;

/**
 * author：kuma
 * project：GridPersonnelAdapter
 * package：com.longxi.tianjintianbao.ui.adapter
 * email：703391768@qq.com
 * data：18/7/16
 */
public class GridPersonnelAdapter extends
        RecyclerView.Adapter<GridPersonnelAdapter.ViewHolder> {
    public static final int TYPE_CAMERA = 1;
    public static final int TYPE_PICTURE = 2;
    private LayoutInflater mInflater;
    private List<Personnel> list = new ArrayList<>();
    private int selectMax = 9;
    private Context context;
    /**
     * 点击添加图片跳转
     */
    private onAddPerClickListener mOnAddPicClickListener;

    public interface onAddPerClickListener {
        void onAddPerClick();
        void onDeletePer(Personnel personnel);
        void onPreview(int position);
    }

    public GridPersonnelAdapter(Context context, onAddPerClickListener mOnAddPicClickListener) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.mOnAddPicClickListener = mOnAddPicClickListener;
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    public void setList(List<Personnel> list) {
        this.list = list;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView mIvHead;
        TextView mTvName;
        ImageView mIvDelete;

        public ViewHolder(View view) {
            super(view);
            mIvHead = (ImageView) view.findViewById(R.id.ivHead);
            mTvName = (TextView) view.findViewById(R.id.tvName);
            mIvDelete = (ImageView) view.findViewById(R.id.ivDelete);
        }
    }

    @Override
    public int getItemCount() {
        if (list.size() < selectMax) {
            return list.size() + 1;
        } else {
            return list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_add_personnel,
                viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    private boolean isShowAddItem(int position) {
        int size = list.size() == 0 ? 0 : list.size();
        return position == size;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        //少于8张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            viewHolder.mIvHead.setImageResource(R.mipmap.ic_add_personnel);
            viewHolder.mTvName.setText("");
            viewHolder.mIvHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnAddPicClickListener.onAddPerClick();
                }
            });
            viewHolder.mIvDelete.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.mIvHead.setImageResource(R.mipmap.ic_no_head);
            viewHolder.mIvHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnAddPicClickListener.onPreview(position);
                }
            });
            viewHolder.mIvDelete.setVisibility(View.VISIBLE);
            viewHolder.mIvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int index = viewHolder.getAdapterPosition();
                    // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
                    // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
                    if (index != RecyclerView.NO_POSITION) {
                        mOnAddPicClickListener.onDeletePer(list.get(index));
                        list.remove(index);
                        notifyItemRemoved(index);
                        notifyItemRangeChanged(index, list.size());
                    }
                }
            });
            Personnel personnel = list.get(position);
            viewHolder.mTvName.setText(personnel.getDept() + "\n" + personnel.getUsername());
            //itemView 的点击事件
            if (mItemClickListener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int adapterPosition = viewHolder.getAdapterPosition();
                        mItemClickListener.onItemClick(adapterPosition, v);
                    }
                });
            }
        }
    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}
